<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<%
	String path = request.getContextPath();
%>
<title>签约管理</title>
<link rel="stylesheet" type="text/css"
	href="<%=path%>/css/btn_class.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/extjs/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/extjs/style.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/extjs/ext-patch.css" />

<script type="text/javascript"
	src="<%=path%>/extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="<%=path%>/extjs/ext-all.js"></script>
 <script type="text/javascript" src="<%=path%>/ext-2.2/build/locale/ext-lang-zh_CN.js"></script>

<link rel="stylesheet" type="text/css"
	href="<%=path%>/extjs/lovcombo-1.0/lovcombo.css" />
<script type="text/javascript" src="<%=path%>/extjs/lovcombo-1.0/lovcombo.js"></script>

	<script type="text/javascript" src="<%=path%>/public/public.js" defer="defer"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
	<div id="west-div" style="width: 100%; height: 100%;"></div>
	<div id="center-div"></div>
</body>
<script type="text/javascript">
	Ext.BLANK_IMAGE_URL = "<%=path%>/extjs/resources/images/default/s.gif";

	Ext.onReady(function() {
				Ext.QuickTips.init();
				
				var Jthtwb = Ext.data.Record.create([ {
					name : 'fpid',
					mapping : 'fpid',
					type : 'string'
				}, {
					name : 'fname',
					mapping : 'fname',
					type : 'string'
				}, {
					name : 'fstate',
					mapping : 'fstate',
					type : 'string'
				}, {
					name : 'fsex',
					mapping : 'fsex',
					type : 'string'
				}, {
					name : 'fphone1',
					mapping : 'fphone1',
					type : 'string'
				}, {
					name : 'fphone2',
					mapping : 'fphone2',
					type : 'string'
				}, {
					name : 'fybkh',
					mapping : 'fybkh',
					type : 'string'
				}, {
					name : 'fidcd',
					mapping : 'fidcd',
					type : 'string'
				}, {
					name : 'fbdate',
					mapping : 'fbdate',
					type : 'string'
				}, {
					name : 'fedate',
					mapping : 'fedate',
					type : 'string'
				}, {
					name : 'fsign_doctor',
					mapping : 'fsign_doctor',
					type : 'string'
				}, {
					name : 'fadd_detail',
					mapping : 'fadd_detail',
					type : 'string'
				}, {
					name : 'fxyjc',
					mapping : 'fxyjc',
					type : 'string'
				}, {
					name : 'fxtjc',
					mapping : 'fxtjc',
					type : 'string'
				}, {
					name : 'fxzjc',
					mapping : 'fxzjc',
					type : 'string'
				}, {
					name : 'fxnsjc',
					mapping : 'fxnsjc',
					type : 'string'
				}, {
					name : 'fxjgjc',
					mapping : 'fxjgjc',
					type : 'string'
				}, {
					name : 'fxybhdjc',
					mapping : 'fxybhdjc',
					type : 'string'
				}, {
					name : 'fxdjc',
					mapping : 'fxdjc',
					type : 'string'
				}, {
					name : 'fydjc',
					mapping : 'fydjc',
					type : 'string'
				}, {
					name : 'foffice',
					mapping : 'foffice',
					type : 'string'
				} ]);

				var ds = new Ext.data.Store({
					reader : new Ext.data.JsonReader({
						totalProperty : 'totalProperty',
						root : 'root'
					}, Jthtwb),
					idProperty : 'threadid',
					remoteSort : false,
					url : 'patient!queryPatientSigned.do'
				});

				ds.load( {
					params : {
						start : 0,
						limit : 50,
						fstate : '1',
						fkeywords : '',
						foffice : '',
						// fbtime : '',
						fetime : ''
					}
				});


				//定义勾选框，不需要可不必定义
				var sm = new Ext.grid.CheckboxSelectionModel({
					singleSelect : false //单选
				});
				
				//定义列
				var colM = new Ext.grid.ColumnModel([ sm, new Ext.grid.RowNumberer(),//增加自动编号，不需要可不必定义
				    {
						header : '合约起始日期',
						dataIndex : 'fbdate',
						width : 100,
						sortable : true,
						renderer : function(value) {
								if (value == null || value == "null") {
									return "";
								} else {
									return value;
								}
							}
					},{
						header : '合约终止日期',
						dataIndex : 'fedate',
						width : 100,
						sortable : true,
						renderer : function(value) {
								if (value == null || value == "null") {
									return "";
								} else {
									return value;
								}
							}
					},{
						header : '身份证号',
						dataIndex : 'fidcd',
						width : 150,
						renderer : function(value) {
								if (value == null || value == "null") {
									return "";
								} else {
									return value;
								}
							}
					},{
						header : '医保卡号',
						dataIndex : 'fybkh',
						width : 150,
						renderer : function(value) {
								if (value == null || value == "null") {
									return "";
								} else {
									return value;
								}
							}
					}, {
						header : '姓名',
						dataIndex : 'fname',
						width : 80,
						sortable : true,
						renderer : function(value) {
								if (value == null || value == "null") {
									return "";
								} else {
									return value;
								}
							}
					}, {
						header : '性别',
						dataIndex : 'fsex',
						width : 60,
						sortable : true,
						renderer : function(value) {
								if (value == "1" || value == "男") {
									return "男";
								} else if (value == "2" || value == "女") {
									return "女";
								} else {
									return "";
								}
							}
					},{
						header : '合约手机号码',
						dataIndex : 'fphone1',
						width : 100,
						sortable : true,
						renderer : function(value) {
								if (value == null || value == "null") {
									return "";
								} else {
									return value;
								}
							}
					},{
						header : '常用手机号码',
						dataIndex : 'fphone2',
						width : 100,
						sortable : true,
						renderer : function(value) {
								if (value == null || value == "null") {
									return "";
								} else {
									return value;
								}
							}
					},{
						header : '签约状态',
						dataIndex : 'fstate',
						width : 80,
						sortable : true,
						renderer : function(value) {
								if(value == "1") {
									return "<img src='<%=path%>/ext-2.2/icons/flag_blue.png' border=0>";
								} else {
									return "<img src='<%=path%>/ext-2.2/icons/flag_null.png' border=0>";
									// return "";
								}
							}
					}, {
						header : '血压监测',
						dataIndex : 'fxyjc',
						width : 80,
						renderer : function(value) {
								if (value == "1" || value == "on") {
									return "<img src='<%=path%>/ext-2.2/icons/tick.png' border=0>";
								} else {
									return "<img src='<%=path%>/ext-2.2/icons/cross.png' border=0>";
								}
							}
					}, {
						header : '血糖监测',
						dataIndex : 'fxtjc',
						width : 80,
						renderer : function(value) {
								if (value == "1" || value == "on") {
									return "<img src='<%=path%>/ext-2.2/icons/tick.png' border=0>";
								} else {
									return "<img src='<%=path%>/ext-2.2/icons/cross.png' border=0>";
								}
							}
					}, {
						header : '血脂',
						dataIndex : 'fxzjc',
						width : 80,
						renderer : function(value) {
								if (value == "1" || value == "on") {
									return "<img src='<%=path%>/ext-2.2/icons/tick.png' border=0>";
								} else {
									return "<img src='<%=path%>/ext-2.2/icons/cross.png' border=0>";
								}
							}
					}, {
						header : '血尿酸',
						dataIndex : 'fxnsjc',
						width : 80,
						renderer : function(value) {
								if (value == "1" || value == "on") {
									return "<img src='<%=path%>/ext-2.2/icons/tick.png' border=0>";
								} else {
									return "<img src='<%=path%>/ext-2.2/icons/cross.png' border=0>";
								}
							}
					}, {
						header : '血肌酐',
						dataIndex : 'fxjgjc',
						width : 80,
						renderer : function(value) {
								if (value == "1" || value == "on") {
									return "<img src='<%=path%>/ext-2.2/icons/tick.png' border=0>";
								} else {
									return "<img src='<%=path%>/ext-2.2/icons/cross.png' border=0>";
								}
							}
					}, {
						header : '血氧饱和度',
						dataIndex : 'fxybhdjc',
						width : 100,
						renderer : function(value) {
								if (value == "1" || value == "on") {
									return "<img src='<%=path%>/ext-2.2/icons/tick.png' border=0>";
								} else {
									return "<img src='<%=path%>/ext-2.2/icons/cross.png' border=0>";
								}
							}
					}, {
						header : '心电',
						dataIndex : 'fxdjc',
						width : 80,
						renderer : function(value) {
								if (value == "1" || value == "on") {
									return "<img src='<%=path%>/ext-2.2/icons/tick.png' border=0>";
								} else {
									return "<img src='<%=path%>/ext-2.2/icons/cross.png' border=0>";
								}
							}
					}, {
						header : '位置服务',
						dataIndex : 'fwzfw',
						width : 80,
						renderer : function(value) {
								if (value == "1" || value == "on") {
									return "<img src='<%=path%>/ext-2.2/icons/tick.png' border=0>";
								} else {
									return "<img src='<%=path%>/ext-2.2/icons/cross.png' border=0>";
								}
							}
					}  ]);

				
				var btn_add_qy = new Ext.Button({
					text : '新增合约',
					iconCls : 'sy-add',
					handler : function() {
						sign_add_fun('90210');
					}
				});
				
				var btn_cancel_qy = new Ext.Button({
					text : '解除合约',
					iconCls : 'sy-delete',
					handler : function() {
						var records = grid.getSelectionModel().getSelected();
						if (records) {
							var array = sm.getSelections();
							var fidstr = "";
							var fnameStr = "";
							Ext.each(array, function(record) {
								fidstr += "'" + record.data.fpid + "', ";
								fnameStr +=  record.data.fname + ", ";
							});
							
							fidstr = fidstr + "'-1'";
							
							Ext.Msg.confirm(
								'确认','您确定<font color=blue>' + fnameStr + '</font>解除合约吗？',
								function(btn) {
									if (btn == 'yes') {
										Ext.Ajax.request( {
												url : 'patient!changeSignState.do',
												params:{
								    	        	fpid : fidstr,
								    	        	fstate : '0'
									    	    },
												success : function() {
													ds.reload();
													Ext.MessageBox
															.alert(
																	'提示',
																	'成功解除合约！');
												},
												failure : function() {
													Ext.Msg
															.show( {
																title : '错误提示',
																msg : '解除合约失败！！',
																buttons : Ext.Msg.OK,
																icon : Ext.Msg.ERROR
															});
												}
											});
								}
							});
					} else {
						Ext.MessageBox.alert('提示', '请选择一行记录进行操作！');
						return;
					}
					}
				});
				
				var btn_go_qy = new Ext.Button({
					text : '续签合约',
					iconCls : 'sy-sure',
					handler : function() {
						var records = grid.getSelectionModel().getSelected();
						if (records) {
							var array = sm.getSelections();
							var fidstr = "";
							var fnameStr = "";
							Ext.each(array, function(record) {
								fidstr += "'" + record.data.fpid + "', ";
								fnameStr +=  record.data.fname + ", ";
							});
							
							fidstr = fidstr + "'-1'";
							
							Ext.Msg.confirm(
								'确认','您确定<font color=blue>' + fnameStr + '</font>续签合约吗？',
								function(btn) {
									if (btn == 'yes') {
										Ext.Ajax.request( {
												url : 'patient!changeSignState.do',
												params:{
								    	        	fpid : fidstr,
								    	        	fstate : '1'
									    	    },
												success : function() {
													ds.reload();
													Ext.MessageBox
															.alert(
																	'提示',
																	'成功续签合约，请双击记录修改合约信息！');
												},
												failure : function() {
													Ext.Msg
															.show( {
																title : '错误提示',
																msg : '续签合约失败！！',
																buttons : Ext.Msg.OK,
																icon : Ext.Msg.ERROR
															});
												}
											});
								}
							});
					} else {
						Ext.MessageBox.alert('提示', '请选择一行记录进行操作！');
						return;
					}
					}
				});
				
				
				var btn_delete_qy = new Ext.Button({
					text : '删除信息',
					iconCls : 'sy-delete',
					handler : function() {
						var records = grid.getSelectionModel().getSelected();
						if (records) {
							var array = sm.getSelections();
							var fidstr = "";
							var fnameStr = "";
							Ext.each(array, function(record) {
								fidstr += "'" + record.data.fpid + "', ";
								fnameStr +=  record.data.fname + ", ";
							});
							
							fidstr = fidstr + "'-1'";
							
							Ext.Msg.confirm(
								'确认','您确定删除<font color=blue>' + fnameStr + '</font>的签约信息吗，删除后数据将无法恢复？',
								function(btn) {
									if (btn == 'yes') {
										Ext.Ajax.request( {
												url : 'patient!deleteSignInfo.do',
												params:{
								    	        	fpid : fidstr,
								    	        	fstate : '0'
									    	    },
												success : function() {
													ds.reload();
													Ext.MessageBox
															.alert(
																	'提示',
																	'删除成功！');
												},
												failure : function() {
													Ext.Msg
															.show( {
																title : '错误提示',
																msg : '删除失败！！',
																buttons : Ext.Msg.OK,
																icon : Ext.Msg.ERROR
															});
												}
											});
								}
							});
					} else {
						Ext.MessageBox.alert('提示', '请选择一行记录进行操作！');
						return;
					}
					}
				});
				
				var btn_export_excel = new Ext.Button({
					text : '导出Excel',
					iconCls : 'sy-white_excel',
					handler : function() {
						//处理列名称
						var headNameStr = "";
						for(var i = 1; i < colM.getColumnCount(true); i++){  
							headNameStr += colM.getColumnHeader(i);   /*得到所有列头信息并放入headNameStr中*/
							if(i == 1 || i == colM.getColumnCount(true)-1 ) {
								headNameStr += "";   
							} else {
								headNameStr += ",";       /*添加分隔符*/
							}
				  		}
						
						var record = grid.getSelectionModel().getSelected();
						var xhStr = "";
						if (record) {
							var array = sm.getSelections();
							
							Ext.each(array, function(record) {
								for(var j = 1; j < colM.getColumnCount(true); j++){ 
									xhStr += record.get(colM.getDataIndex(j)) + ",";
								}
								xhStr += "&";
							});
							
							printExcel_form(headNameStr, xhStr); 
						} else {
							Ext.MessageBox.alert('提示', '请选择一条或多条记录进行操作！');
						}			
					}
				});
			
			
			function printExcel_form(headNameStr, xhStr) {
				
				var f = new Ext.FormPanel({
					frame:true,
			        layout:"form",
			        buttonAlign:'center',
			        onSubmit: Ext.emptyFn,
			        submit: function() {
			                this.getEl().dom.action= 'patient!printExcel.do',
			                this.getEl().dom.method = 'post',
			                this.getEl().dom.submit();
			        },
			      
			        items : [ {
						layout : 'column',
						labelAlign : 'center',
						border : false,
						frame : true,
						//    autoHeight : true,
						bodyStyle : 'padding-right:5px;padding-left:5px;padding-top:5px;',
						items : [ {
							html : '<b>导出列:</b>',
							height: 26,
							anchor : '98%'
					}, {
						html : headNameStr,
						anchor : '98%'
				    }, {
							xtype : 'hidden',
							fieldLabel : '列名称',
							name : 'headNameStr',
							readOnly : false,
							allowBlank : true,
							emptyText : '',
							value : headNameStr,
							anchor : '98%'
					}, {
						xtype : 'hidden',
						fieldLabel : '数据',
						id : 'xhStr',
						name : 'xhStr',
						readOnly : false,
						allowBlank : true,
						emptyText : '',
						value : xhStr,
						anchor : '98%'
					}]
						}]
				});
		
				var btnSubmit = new Ext.Button({
			        text: "提交",
			        iconCls : 'sy-save',
			        handler: function(){
			             f.getForm().submit();
			             role_detail_window.close();
			        }
				});
				
				var btnReset = new Ext.Button({
			        text: "取消",
			        iconCls : 'sy-reset',
			        handler: function(){
			        	role_detail_window.close();
			        }
				});
				
				var role_detail_window = new Ext.Window({
					tbar : [ btnSubmit, '-', btnReset],
					width : 380,
					height : 190,
					title : '导出到Excel',
					iconCls : 'sy-win-flag',
					modal : true,
					items : [ f ]
				});
				role_detail_window.show();
			}
			
			
				var btn_refresh_office = new Ext.Button({
					text : '刷新',
					iconCls : 'sy-refresh',
					handler : function(btn) {
						
					    var state = "0";
					    
					    if(Ext.getCmp('check_state_id').checked) {
					    	state = "1";
					    }
					  
						ds.removeAll();
						ds.on('beforeload', function(store) {
							this.baseParams = {
									'limit' : 500,
									'foffice' : Ext.get("foffice_id").dom.value
								};
						});
				
						ds.load({
							params : {
								start : 0,
								fstate : state,
								fkeywords : Ext.get('fkeywords_id').dom.value,
							//	fbtime : Ext.get('fbtime_id').dom.value,
								fetime : Ext.get('fetime_id').dom.value
							}
						});
					
					}
				});
				
				//生成表格
				var grid = new Ext.grid.GridPanel({
					closable : false,
					region : 'center',
					height : 350,
					autoWidth : true,
					stripeRows : true,
					//title : '角色列表',
					cm : colM,
					ds : ds,
					sm : sm,
					listeners : {
						'rowdblclick' : function(grid, row) { //双击打开
							var record = grid.getSelectionModel().getSelected();
							if (record) {
								sign_mod_fun(record.data.foffice, record.data.fpid, 
								             record.data.fbdate, 
								             record.data.fedate, 
								             "", 
								             record.data.fybkh,
								             record.data.fidcd, 
								             record.data.fname, 
								             record.data.fsex, 
								             record.data.fadd_detail, 
								             record.data.fphone1, 
								             record.data.fphone2, 
								             record.data.fxyjc,
								             record.data.fxtjc,
								             record.data.fxzjc,
								             record.data.fxnsjc,
								             record.data.fxjgjc,
								             record.data.fxybhdjc,
								             record.data.fydjc,
								             record.data.fxdjc,
								             record.data.fwzfw
								         );
							}
						}
					},
					
					tbar : [ btn_add_qy, '-', btn_cancel_qy, '-', btn_go_qy, '-', btn_export_excel , '-', btn_refresh_office, '-', btn_delete_qy],
					bbar : new Ext.PagingToolbar({
				        pageSize: 50,
				        store: ds,
				        displayInfo: true,
				        displayMsg: '显示 {0} 到 {1} 条记录，共 {2} 条',
				        emptyMsg: "没有记录"
				    })
				});
				
			var e_date = new Ext.form.DateField({
	   				fieldLabel : '合约终止日期',
	   				format: 'Y-m-d',
	   				id:'fetime_id',
					name:'fedate',
					width : 100,
	   				style:"cursor:pointer;color:blue"
	   			});
			
	   			
	   			//是否包含解约信息
			    var state_checkbox = new Ext.form.Checkbox({
		              id:'check_state_id',
		               name:'fstate',   
		               fieldLabel:'包含解约患者',   
		               boxLabel:'包含已解约患者',
		               checked:false
		        });
	   			
	   			var btn_search_patient = new Ext.Button({
					text : '查询',
					iconCls : 'sy-search',
					handler : function(btn) {
					    var state = "0";
					    
					    if(Ext.getCmp('check_state_id').checked) {
					    	state = "1";
					    }
					  
						ds.removeAll();
						ds.on('beforeload', function(store) {
							this.baseParams = {
									'limit' : 500,
									'foffice' : Ext.get("foffice_id").dom.value
								};
						});
				
						ds.load({
							params : {
								start : 0,
								fstate : state,
								fkeywords : Ext.get('fkeywords_id').dom.value,
								// fbtime : Ext.get('fbtime_id').dom.value,
								fetime : Ext.get('fetime_id').dom.value
							}
						});
					
					}
				});
				
			    var btn_send_mess = new Ext.Button({
					text : '发送短信',
					iconCls : 'sy-send-info',
					handler : function() {
					var content = Ext.get("fcontent_q_id").dom.value;
					if(content == null || content == "") {
						Ext.MessageBox.alert('提示', '请编辑短信内容！');
						return false; 
					}
					
					var record = grid.getSelectionModel().getSelected();
					if (record) {
							var array = sm.getSelections();
							var phoneStr = "";
							Ext.each(array, function(jhmy_record) {
								phoneStr += record.data.fphone + ",";
							});
							if (phoneStr.length == 0) {
									phoneStr = "";
							} else {
								phoneStr = phoneStr.substring(0,
										phoneStr.length - 1);
							}
							
							Ext.Ajax.request( {
								url : 'mess!sendMess.do',
								params:{
				    	        	'fphone' : phoneStr,
				    	        	'fcontent' : content
					    	    },
								success : function() {
									Ext.MessageBox.alert(
													'提示',
													'发送成功！');
								},
								failure : function() {
									Ext.Msg.show( {
										title : '错误提示',
										msg : '发送失败！！',
										buttons : Ext.Msg.OK,
										icon : Ext.Msg.ERROR
									});
								}
							});
						} else {
							Ext.MessageBox.alert('提示', '请选择一行记录进行操作！');
						}
					}
				});
				
				var btn_mess_clean = new Ext.Button({
					text : '清空内容',
					iconCls : 'sy-email-error',
					handler : function() {
						Ext.get("fcontent_q_id").dom.value  = "";
					}
				});
				
				var btn_mess_temp = new Ext.Button({
					text : '短信模板',
					iconCls : 'sy-email_link',
					handler : function() {
					
					
					}
				});
			    
				 var twoTbar = new Ext.Toolbar({
					items:[ '快发信息：',
						    {
						xtype : 'textarea',
						id : 'fcontent_q_id',
						name : 'fcontent',
						style:"cursor:pointer;color:blue;",
						height : 50,
						width : 500
					}, '-', btn_send_mess , '-', btn_mess_clean // , '-', btn_mess_temp  
				    ]
			    });
				
			    
			    var panel = new Ext.Panel({
			    	region: "center",
					layout:'fit',
					border : true,
					margins:'0 5 0 0',
					tbar : ['合约终止日期 <b> < <b/>', e_date, '-', '关键字：', {
						xtype : 'textfield',
						fieldLabel : '关键字',
						id : 'fkeywords_id',
						name : 'fkeywords',
						style:"cursor:pointer;color:blue",
						width : 130
					}, '-', state_checkbox, '-', btn_search_patient, {
						xtype : 'hidden',
						fieldLabel : '机构代码',
						id : 'foffice_id',
						name : 'foffice'
						
					} ],
					listeners : {
						'render' : function(){
							// oneTbar.render(this.tbar); 
							twoTbar.render(this.tbar); 
						}
				 	},
					items : [ grid]
				});
				
				
				/************************************* 所属区县树 结束 ********************************************************/
				var viewport = new Ext.Viewport({
					layout : 'border',
					items : [ panel ]
				});
				
				Ext.getCmp('fkeywords_id').focus(false, 300); //解决焦点无法获取问题 ， 使光标可以定位到选框
			});
</script>


</html>